package system

import (
	"context"
	"zuhao-seucre/internal/consts"
	"zuhao-seucre/internal/dao"
	"zuhao-seucre/internal/model/input"
	"zuhao-seucre/internal/service"

	"zuhao-seucre/api/system/v1"
)

func (c *ControllerV1) Permissions(ctx context.Context, req *v1.PermissionsReq) (res *v1.PermissionsRes, err error) {
	var (
		resources   []*input.AccountResourceVO
		permissions []string
		menus       []*input.AccountResourceVO
	)
	user := service.ContextX().GetUser(ctx)

	if user.UserId == consts.AdminId {
		err = dao.TSysResource.Ctx(ctx).Where(dao.TSysResource.Columns().Status, consts.ResourceStatusEnable).OrderAsc(dao.TSysResource.Columns().Pid).OrderAsc(dao.TSysResource.Columns().Weight).Scan(&resources)
	} else {

	}
	// 封装想要的参数
	for _, v := range resources {
		if v.Type == consts.ResourceTypeMenu {
			menus = append(menus, v)
		} else {
			permissions = append(permissions, *v.Code)
		}
	}
	res = new(v1.PermissionsRes)
	res.Menus = input.TreeList(0, menus)
	res.Permissions = permissions
	return
}
